package com.vincent.seeyou.ui;

import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vincent.seeyou.R;
import com.vincent.seeyou.model.User;
import com.vincent.seeyou.util.ImageLoadOptions;

import org.w3c.dom.Text;

import java.util.List;

import cn.bmob.v3.listener.FindListener;

public class PersonInfoActivity extends BaseAndCheckInActivity {

    private ImageView imageView_avatar;
    private TextView textView_account;
    private TextView textView_nick_name;
    private TextView textView_gender;

    private User user;
    private String userName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info);
        userName = getIntent().getStringExtra("userName");
        initView();
        initData();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.person_info, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView(){
        imageView_avatar = (ImageView)findViewById(R.id.imageView_avatar);
        textView_account = (TextView)findViewById(R.id.textView_account);
        textView_nick_name = (TextView)findViewById(R.id.textView_nick_name);
        textView_gender = (TextView)findViewById(R.id.textView_gender);
    }

    private void initData(){
        bmobUserManager.queryUserInfo(userName, new FindListener<User>() {

            @Override
            public void onError(int arg0, String arg1) {
                // TODO Auto-generated method stub
                ShowLog("onError onError:" + arg1);
            }

            @Override
            public void onSuccess(List<User> arg0) {
                // TODO Auto-generated method stub
                if (arg0 != null && arg0.size() > 0) {
                    user = arg0.get(0);
                    updateUser(user);
                } else {
                    ShowLog("onSuccess 查无此人");
                }
            }
        });
    }
    private void updateUser(User user) {
        // 更改
        refreshAvatar(user.getAvatar());
        textView_account.setText(user.getUsername());
        textView_nick_name.setText(user.getNick());
        textView_gender.setText(user.getSex() == true ? "男" : "女");

    }

    private void refreshAvatar(String avatar) {
        if (avatar != null && !avatar.equals("")) {
            ImageLoader.getInstance().displayImage(avatar, imageView_avatar,
                    ImageLoadOptions.getOptions());
        } else {
            imageView_avatar.setImageResource(R.drawable.ic_launcher);
        }
    }

    public void onChat(View view) {
        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra("user", user);
        startAnimActivity(intent);
        finish();
    }
}
